package com.pi.pro.xcmg.am.was.web.cust.controller;

import com.pi.common.CodesConstants;
import com.pi.common.utils.TextUtils;
import com.pi.framework.core.controller.BaseCrudController;
import com.pi.framework.core.service.ICrudService;
import com.pi.framework.core.util.SecurityUtils;
import com.pi.framework.core.util.StringUtils;
import com.pi.wms.common.WmsConstants;
import com.pi.wms.dto.act.PalletizeActionDto;
import com.pi.wms.dto.inventory.InventoryLabelDto;
import com.pi.wms.entity.*;
import com.pi.wms.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.datatables.mapping.DataTablesInput;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.*;


@Slf4j
@Controller
@RequestMapping("/inbound/kitdocline")
public class MesKitDoclineController extends BaseCrudController<TwiDocMesKitDocline, Long> {

    @Autowired
    protected LocationService locationService;

	@Autowired
    protected MesKitDocklineService service;

	@Autowired
    protected AsnService headerService;

	@Autowired
	private ParameterService parameterService;

    @Autowired
	protected PalletizeService palletizeService;


	@Autowired
	protected PrintTaskService printTaskService;
	
	
	@Override
	protected ICrudService<TwiDocMesKitDocline, Long> getCrudService() {
	    return service;
	}

	@Override
	protected String getPrefix() {
		return "kitdocline";
	}


	@Override
	protected void prepareDatatableConditions(Map<String, Object> query, DataTablesInput input) {
		Long userId = SecurityUtils.getLoginUserId();
		query.put("userId", userId.toString());
		super.prepareDatatableConditions(query, input);
	}
	
	@Override
	@GetMapping(value = { "", "/{docnumber}" })
	public String list(Model model, @PathVariable Map<?, ?> params, HttpServletRequest request,
			RedirectAttributes redirectAttributes) {
		TwaDocAsnHeader header = this.getHeader(params);
		model.addAttribute("r", header);
		if (header!=null){
			Map<String,Object> maps=new HashMap<String,Object>();
			maps.put("m.headerId", header.getId());
			model.mergeAttributes(maps);
		}
        this.setParams(request, params);
        model.addAttribute("params", params);
        return this.view(this.getPrefix() + "_list");
	}

    private TwaDocAsnHeader getHeader(HttpServletRequest request){
		Object headerId = getParams(request).get("headerId");
		return this.getHeader(headerId);
    }

    private TwaDocAsnHeader getHeader(Map<?,?> params){
		Object headerId = params.get("headerId");
		return this.getHeader(headerId);
    }
    
    private TwaDocAsnHeader getHeader(Object headerId){
    	TwaDocAsnHeader header = new TwaDocAsnHeader();
		if (null != headerId && StringUtils.isNotBlank(headerId.toString())) {
			header = headerService.getById(Long.valueOf(headerId.toString()));
		}
		return header;
    }

}
